package com.hlkj.baojin.controller;

import com.hlkj.baojin.entity.dto.CompanyAreaDto;
import com.hlkj.baojin.entity.dto.CompanyAreaSearchDto;
import com.hlkj.baojin.entity.dto.MonitorDto;
import com.hlkj.baojin.service.ICompanyAreaService;
import com.hlkj.baojin.utils.ValidationUtil;
import com.hlkj.baojin.utils.bean.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author HULIN
 * @created with: baojin
 * @package com.hlkj.baojin.controller
 * @description:
 * @date 2019/1/9 10:45
 * @modified By:
 * @Copyright © 2018 HAILIANG Info. Tech Ltd. All rights reserved.
 */
@Api(value = "/bjjk", description = "公司管理")
@RestController
public class CompanyAreaController {

    @Autowired
    private ICompanyAreaService companyAreaService;

    @ApiOperation(value = "厂区列表", tags = {"厂区管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = CompanyAreaDto.class))
    @PostMapping(value = "/companyArea/list")
    public ResponseEntity<JsonResult> companyAreaList(@RequestBody CompanyAreaSearchDto companyAreaSearchDto) {
        return ResponseEntity.ok(JsonResult.success(companyAreaService.findCompanyArea(companyAreaSearchDto)));
    }

    @ApiOperation(value = "厂区保存", tags = {"厂区管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = void.class))
    @PostMapping(value = "/companyArea/save")
    public ResponseEntity<JsonResult> companyAreaSave(@RequestBody CompanyAreaDto companyAreaDto) {
        ValidationUtil.validate(companyAreaDto);

        for (MonitorDto monitorDto : companyAreaDto.getMonitorDtoList()) {
            ValidationUtil.validate(monitorDto);
        }

        companyAreaService.saveCompanyArea(companyAreaDto);
        return ResponseEntity.ok(JsonResult.success());
    }

    @ApiOperation(value = "厂区更新", tags = {"厂区管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = void.class))
    @PostMapping(value = "/companyArea/update")
    public ResponseEntity<JsonResult> companyAreaUpdate(@RequestBody CompanyAreaDto companyAreaDto) {
        companyAreaService.updateCompanyArea(companyAreaDto);
        return ResponseEntity.ok(JsonResult.success());
    }

    @ApiOperation(value = "厂区删除", tags = {"厂区管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = void.class))
    @PostMapping(value = "/companyArea/delete/{companyAreaId}")
    public ResponseEntity<JsonResult> companyAreaDelete(@PathVariable("companyAreaId") String companyAreaId) {
        companyAreaService.deleteCompanyArea(companyAreaId);
        return ResponseEntity.ok(JsonResult.success());
    }


    @ApiOperation(value = "厂区下拉列表", tags = {"厂区管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = CompanyAreaDto.class))
    @GetMapping(value = "/companyArea/list/{companyId}")
    public ResponseEntity<JsonResult> findCompanyAreaList(@PathVariable("companyId") String companyId) {

        return ResponseEntity.ok(JsonResult.success(companyAreaService.findCompanyAreaList(companyId)));
    }


    @ApiOperation(value = "监控列表", tags = {"厂区管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = CompanyAreaDto.class))
    @GetMapping(value = "/companyArea/monitor/list/{companyAreaId}")
    public ResponseEntity<JsonResult> findMonitorByCompanyAreaId(@PathVariable("companyAreaId") String companyAreaId) {

        return ResponseEntity.ok(JsonResult.success(companyAreaService.findMonitorByCompanyAreaId(companyAreaId)));
    }
}
